//turret.txt - Simple script for turrets
//Cell 0,1 - Stuff done flag. If both 0, nothing. Otherwise If non-zero, this 
//turret wont attack. If the turret is itself attacked, will set the not-attack setting back to 0.
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

short i,target,r1;
short create_count = 0;
short last_spawn,last_damage,last_heal;
short summon_mess = 0;
short mess1 = 0;
short mess2 = 0;

body;

beginstate INIT_STATE;
	set_name(ME,"General Alwan");
	set_level(ME,34);
	change_max_health(ME,400);
	set_boss_level(ME,2);
	set_attack_bonus(ME,12);
	last_spawn = get_current_tick();
	last_damage = get_current_tick();
	last_heal = get_current_tick();
	set_walk_speed(ME,0);
	
	set_char_unkillable(ME,1);
	
	if (gf(100,16) > 0)
		erase_char(ME);
	if (gf(101,4) > 0)
		erase_char(ME);
	break;

beginstate DEAD_STATE;
	sf(101,4,1);
	begin_talk_mode(14);
break;

beginstate START_STATE; 
		if ((get_attitude(ME) >= 10) && (get_health(ME) < get_max_health(ME)) && (tick_difference(last_heal,get_current_tick()) > 2) && (get_nearest_party_char(7) < 0)) {
				print_str_color("Alwan casts a healing spell.",2);
				heal_char(ME,1000);
				pc_heard_sound_delay(100,250);						
				run_char_animation(2,1,50);	
				last_heal = get_current_tick();
					
				end();
				}
	if ((gf(42,15) == 0) || (gf(42,16) == 0) || (gf(42,17) == 0) || (gf(42,18) == 0)) {
		i = 0; // just dummy line
		}
		else {
			set_char_unkillable(ME,0);
			}
			
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		last_spawn = get_current_tick();
		do_attack();
		set_state(3);
		}
		
	if (get_foe_target(ME,10,0)) {
		last_spawn = get_current_tick();
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking

		
	if (target_ok() == FALSE)
		set_state(START_STATE);
	
	if ((gf(42,15) == 0) || (gf(42,16) == 0) || (gf(42,17) == 0) || (gf(42,18) == 0)) {
		if ((get_attitude(ME) >= 10) && (get_health(ME) < get_max_health(ME)) && (tick_difference(last_heal,get_current_tick()) > 0)) {
			print_str_color("Alwan draws power from the pylons. They keep him alive.",2);
			if (is_combat())
				heal_char(ME,500);
				else heal_char(ME,1000);
			pc_heard_sound_delay(100,250);						
			run_char_animation(2,1,50);	
			last_heal = get_current_tick();
			
			if (gf(42,19) == 0) {
				sf(42,19,1);
				begin_talk_mode(15);
				}
				
			end();
			}
		}
		else {
			set_char_unkillable(ME,0);

			// alwan heals if no pc close
			if ((get_attitude(ME) >= 10) && (get_health(ME) < get_max_health(ME)) && (tick_difference(last_heal,get_current_tick()) > 0) && (get_nearest_party_char(7) < 0)) {
				print_str_color("Alwan casts a healing spell.",2);
				heal_char(ME,1000);
				pc_heard_sound_delay(100,250);						
				run_char_animation(2,1,50);	
				last_heal = get_current_tick();
					
				end();
				}
			}
		
			
	if ((get_attitude(ME) >= 10) && (tick_difference(last_spawn,get_current_tick()) > 0)) {
		i = 0;
		if (char_ok(14))
			i = i + 1;
		if (char_ok(15))
			i = i + 1;
		if (char_ok(16))
			i = i + 1;
		if (char_ok(17))
			i = i + 1;
		if (i < 2) {
			r1 = get_ran(1,0,3);
			if (char_ok(14 + r1))
				r1 = get_ran(1,0,3);
			if (char_ok(14 + r1) == FALSE) {				
				print_named_str(ME,"calls forth a creation!");
				spawn_creature(6 + r1);
				set_summon_level(14 + r1,1);
				place_particle_num(14 + r1,10,7,10);
				set_level(14 + r1,30);
				set_attack_bonus(14 + r1,20);
				
				set_resistance(14 + r1,7,80);
				
				if (r1 == 0) {
					set_name(14 + r1,"Terror Rune Beta");
					place_particle_num(14 + r1,18,12,4);		
					set_resistance(14 + r1,6,100);
					}
				if (r1 == 1) {
					set_name(14 + r1,"Confusion Rune Beta");
					place_particle_num(14 + r1,19,12,4);		
					set_resistance(14 + r1,4,100);
					set_resistance(14 + r1,5,100);
					}
				if (r1 == 2) {
					set_name(14 + r1,"Fire Rune Beta");
					place_particle_num(14 + r1,20,12,4);		
					set_resistance(14 + r1,2,100);
					}
				if (r1 == 3) {
					set_name(14 + r1,"Static Rune Beta");
					place_particle_num(14 + r1,21,12,4);		
					set_resistance(14 + r1,1,100);
					}
				run_char_animation(2,1,50);	
				pc_heard_sound_delay(136,100);						
				last_spawn = get_current_tick();
				end();
				}
			}
		}
		
	if ((tick_difference(last_spawn,get_current_tick()) > 1) && (get_attitude(ME) >= 10)) {
		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"surrounds himself with an aura of power.");
			pc_heard_sound_delay(179,250);						
	  		create_missile_spiral(147,40,6,2);
	  		
	  		damage_nearby(200 + get_ran(1,0,40),6,1,0);
			status_nearby(-10,6,0,0);
			status_nearby(-10,6,8,0);

			last_spawn = get_current_tick();
			end();
			
			}

	do_attack();

	turret_heal();
break;

beginstate TALKING_STATE;
	begin_talk_mode(30);
break;